// pages/mine/account/red_bag.js
var App = getApp();
var startNum = 0;
var hasData = true;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    purchaseList: [],
    contentDis: false//缺省页
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.getMoneyList();
  },

  /**
   * 页面相关事件处理函数--监听用户上拉动作
   */
  onReachBottom: function () {
    if (hasData) {
      startNum += 10;
      this.getMoneyList();
    }
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  //back
  backLastPage: function () {
    wx.navigateBack({
      delta: 1
    })
  },
  //获取红包记录列表
  getMoneyList: function () {
    let that = this;
    let max = 10;
    wx.request({
      url: App.data.globalAPI + '/users/redpacket/getlist',
      data: {
        token: wx.getStorageSync('token'),
        start: startNum,
        num: max
      },
      header: {
        'content-type': 'application/json' // 默认值
      },
      success(res) {
        if (res.data.error == 0) {
          let list = res.data.data.items;
          var origin = that.data.purchaseList;
          hasData = true;
          list.forEach((item) => {
            item.receive_time = App.timeTransformat(item.receive_time)
          })
          origin.push(...list); 
          that.setData({
            purchaseList: origin
          })
          if(origin.length==0){
            hasData = false;
            that.setData({
              contentDis: true
            })
          }else if (list.length < max) {
            hasData = false;
          }
        } else {
          wx.showToast({
            title: res.data.msg,
            icon: 'none',
            duration: 600
          })
        }
      }
    })
  }
})